import api from '../api';
import Vue from 'vue';
import Vuex from 'vuex';
import {Toast, Indicator,MessageBox} from 'mint-ui';
import router from '../router';
import util from '../utils/Util.js';
import weixinUrl from '../utils/WeixinConst.js';
// import Router from 'vue-router'
Vue.use(Vuex);
const state = {
  list: [
    {
      url: 'static/images/recharge1_active.png',
      activeUrl: 'static/images/recharge1_active.png',
      name: '充值缴费',
      isActive: true,
      tips: "直接输入上网账号即手机号"
    },
    {
      url: 'static/images/recharge2.png',
      activeUrl: 'static/images/recharge2_active.png',
      name: '查询余额',
      isActive: false,
      tips: "直接输入上网账号即手机号"
    }
  ],
  title: '选择场所',//头部的标题
  placeList: [],//场所列表
  packageList: [],
  userInfo: {},
  mobile: '',//电话号码
  type: 0,//选择业务的类型，0代表个人充值，1代表充值缴费
  isLoading: false//是否加载动画
};

const actions = {
  getToken(context, params) {
    api.getToken(params).then(function (response) {
      console.log(response);
    }).catch(function (error) {
      console.log(error)
    })
  },
  /***
   * 输入手机号码后，获取场所信息
   * @param context
   * @param obj其中obj包含了电话号码‘tel’和选择功能类型‘type’两个参数
   */
  login(context, obj) {
    let params = {
      "mobile": obj.tel
    };
    localStorage.setItem('type',obj.type);
    context.state.isLoading = true;
    let timer = setTimeout(function () {
      api.login(params).then(function (response) {
        context.state.isLoading = false;
        if (response.data.error === '5005' || response.data.error === 0) {
          context.state.title = '选择场所';
          context.state.placeList = response.data.data;
          localStorage.setItem('placeList', JSON.stringify(response.data.data));
          clearTimeout(timer);
          router.push({
            name: 'place',
            query: {
              mobile: obj.tel
            }
          });
        } else {
          Toast(response.data.message);
        }
      })
    }, 2000)
  },
  /***
   * 当点击场所列表的’下一步‘时候，跳到相应的信息,若是查询缴费类别'0'则跳到'/renew'，类别’1‘则跳转到’/user'
   * @param context
   * @param params，params包含了选择功能的类别，user_id,和place_id
   */
  getPlaceInfo(context, params) {
    params.type = localStorage.getItem('type');
    context.state.isLoading = true;
    let timer = setTimeout(function () {
      api.getPlaceInfo(params).then(function (response) {
        context.state.isLoading = false;
        Indicator.close();
        if (response.data.error === 0) {
          clearTimeout(timer);
          context.state.title = '上网续费';
          if (localStorage.getItem('type') === '0') {
            let list = response.data.data;
            localStorage.setItem('packageList', JSON.stringify(list));
            router.push({
              path: '/renew',
            });
          } else {
            localStorage.setItem('user', JSON.stringify(response.data.data));
            router.push({
              path: '/user',
            });
          }
        } else {
          Toast(response.data.message);
        }
      })
    }, 2000)
  },
  pay(context, data) {
    if (localStorage.getItem('isFirst')) {
      MessageBox.confirm('您已经过了支付失效时间,是否重新登陆', '提示').then(action => {
        location.href = weixinUrl.payUrl;
        return false;
      });
    } else {
      localStorage.setItem('isFirst', '1');
    }
    localStorage.setItem('package', JSON.stringify(data));
    api.pay(data).then(function (response) {
      if (response.data.error === 0) {
        util.weixinPay(JSON.parse(response.data.data));
      }
    })
  }
};

const getters = {
  getterPlaceList() {
    let obj = JSON.parse(localStorage.getItem('placeList'));
    obj.forEach(function(item){
      if(item.hasOwnProperty('procreatorId')){
        item.className = util.getClassName(item.procreatorId);
      } else {
        item.className = util.getClassName(0);
      }
    });
    return obj;
  },
  getterUserInfo() {
    return JSON.parse(localStorage.getItem('userInfo'));
  },
  getterPackageList() {
    let list = JSON.parse(localStorage.getItem('packageList'));
    for (let index = 0; index < list.length; index++) {
      list[index].number = 1;
      list[index].isChecked = false;
    }
    return list;
  }
};
export default new Vuex.Store({
  state: state,
  getters: getters,
  actions: actions
})
